import requests
from bs4 import BeautifulSoup
import time

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0 Safari/537.36'
}

def get_movies(start):
    url = f"https://movie.douban.com/top250?start={start}"
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    items = soup.select('.item')
    
    for item in items:
        title = item.select_one('.title').text
        rating = item.select_one('.rating_num').text
        print(f"电影：{title}, 评分：{rating}")

# 爬取前3页
for i in range(0, 75, 25):
    get_movies(i)
    time.sleep(1)  # 延迟，防止被封
